package org.mx.test.config;

import javax.persistence.EntityManager;

import org.mx.dal.config.DalConfig;
import org.mx.dal.config.DalHibernateConfig;
import org.mx.dal.service.EntityManagerService;
import org.mx.dal.service.GeneralAccessor;
import org.mx.dal.service.GeneralDictAccessor;
import org.mx.dal.service.impl.GeneralAccessorImpl;
import org.mx.dal.service.impl.GeneralDictAccessorImpl;
import org.mx.dbcp.MyDbcp2DataSourceFactoryUtil;
import org.mx.spring.session.SessionDataStore;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * 基于Hibernate的DAL基础实现，从RDB继承而来。
 *
 * @author : john.peng date : 2017/10/7
 * @see DalConfig
 */
@SuppressWarnings("javadoc")
@EnableTransactionManagement
public class DalHibernateTest2Config {
	/**
	 * 创建实体管理器工厂Bean
	 *
	 * @param env       Spring IoC环境
	 * @param util		数据库连接池工厂集合，可以根据配置名访问
	 * @return 实体管理器工厂Bean
	 */
	@Bean("entityManagerFactoryTest2")
	public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(Environment env,
			MyDbcp2DataSourceFactoryUtil util) {
		return DalHibernateConfig.createEntityManagerFactoryBean(env, util, "test2", "jpa2");
	}

	/**
	 * 创建一个实体管理器
	 * @param emfb 实体管理工厂Bean
	 * @return EntityManager
	 */
	@Bean("entityManagerTest2")
	public EntityManager entityManager(
			@Qualifier("entityManagerFactoryTest2") LocalContainerEntityManagerFactoryBean emfb) {
		return emfb.getObject().createEntityManager();
	}

	/**
	 * 创建事务管理器
	 *
	 * @param containerEntityManagerFactoryBean 实体管理器工厂Bean
	 * @return 事务管理器
	 */
	@Bean("transactionManagerTest2")
	public PlatformTransactionManager transactionManager(
			@Qualifier("entityManagerFactoryTest2") LocalContainerEntityManagerFactoryBean containerEntityManagerFactoryBean) {
		return new JpaTransactionManager(containerEntityManagerFactoryBean.getObject());
	}

	/**
	 * 创建基于JPA的数据对象Accessor
	 * 
	 * @param sessionDataStore 会话数据存储
	 * @return Accessor
	 */
	@Bean(name = "generalAccessorJpaTest2")
	public GeneralAccessor generalAccessorJpa(
			@Qualifier("entityManagerServiceTest2") EntityManagerService entityManagerService,
			SessionDataStore sessionDataStore) {
		return new GeneralAccessorImpl(entityManagerService, sessionDataStore);
	}

	/**
	 * 创建基于JPA的字典对象Accessor
	 * 
	 * @param sessionDataStore 会话数据存储
	 * @return Accessor
	 */
	@Bean(name = "generalDictAccessorJpaTest2")
	public GeneralDictAccessor generalDictAccessorJpa(
			@Qualifier("entityManagerServiceTest2") EntityManagerService entityManagerService,
			SessionDataStore sessionDataStore) {
		return new GeneralDictAccessorImpl(entityManagerService, sessionDataStore);
	}

	/**
	 * 创建一个通用的数据访问器
	 *
	 * @param context Spring IoC上下文
	 * @return 数据访问器
	 */
	@Bean(name = "generalAccessorTest2")
	public GeneralAccessor generalAccessor(ApplicationContext context) {
		return context.getBean("generalAccessorJpaTest2", GeneralAccessor.class);
	}

	/**
	 * 创建一个通用的字典数据访问器
	 *
	 * @param context Spring IoC上下文
	 * @return 数据访问器
	 */
	@Bean(name = "generalDictAccessorTest2")
	public GeneralDictAccessor generalDictAccessor(ApplicationContext context) {
		return context.getBean("generalDictAccessorJpaTest2", GeneralDictAccessor.class);
	}
}
